package com.kcsj.service;

import com.kcsj.dao.ProductModelDao;
import com.kcsj.domain.ProductModel;
import com.kcsj.util.Condition;
import com.kcsj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class ProductModelService {
    private static ProductModelDao productModelDao = ProductModelDao.getInstance();
    private static ProductModelService productModelService =new ProductModelService();
    private ProductModelService(){}
    public static ProductModelService getInstance(){
        return productModelService;
    }

    public ProductModel find(String orderNo) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        ProductModel order = null;
        try{
            order = productModelDao.find(orderNo, conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }

        return order;
    }
    public Collection<ProductModel> findByProductNo(String productNo) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<ProductModel> productModels = productModelDao.findByProductNo(productNo, conn);
        JdbcHelper.close(conn);
        return productModels;
    }

    public Collection<ProductModel> findAll(List<Condition> conditionList)throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<ProductModel> deliveryPeoples = productModelDao.findAll(conditionList,connection);
        JdbcHelper.close(connection);
        return deliveryPeoples;
    }


    public boolean update(ProductModel productModel) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        try {
            boolean judge = productModelDao.update(productModel,connection);
            connection.commit();
            return judge;
        } catch (SQLException e) {
            connection.rollback();
            throw e;
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
    }

    public int add(ProductModel productModel) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        try {
            int num = productModelDao.add(productModel,connection);
            connection.commit();
            return num;
        } catch (SQLException e) {
            connection.rollback();
            throw e;
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
    }

    public boolean delete(String productModelNo) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        try {
            boolean judge = productModelDao.delete(productModelNo,connection);
            connection.commit();
            return judge;
        } catch (SQLException e) {
            connection.rollback();
            throw e;
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
    }

    public boolean changeRest(String productModelNo, Integer count) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean status = false;
        try {
            status = productModelDao.changeRest(productModelNo, count, conn);
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return status;

    };

}
